package com.sky.controller.admin;

import com.sky.bo.EditPassWordBo;
import com.sky.bo.EmployeeAddBo;
import com.sky.bo.EmployeeBo;
import com.sky.bo.EmployeeEditBo;
import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Validated
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody @Valid EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        claims.put(JwtClaimsConstant.UUID, UUID.randomUUID().toString());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);
        redisTemplate.opsForValue().set(employee.getId().toString(),token);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     *  分页查询
     * @param employeeBo
     * @return
     */

    @GetMapping("/page")
    public Result<PageResult> page(EmployeeBo employeeBo){
        return Result.success(employeeService.page(employeeBo));
    }


    /**
     * 添加员工
     * @param employeeAddBo
     * @param request
     * @return
     */
    @PostMapping
    public Result addEmployee(@RequestBody @Valid EmployeeAddBo employeeAddBo, HttpServletRequest request) {
        if(employeeService.add(employeeAddBo,request)){
            return Result.success();
        }
        return Result.error("添加失败");
    }


    /**
     * 通过id查看员工详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> getEmployeeById(@PathVariable Long id){
       return Result.success(employeeService.getEmployeeById(id));
    }

    /**
     * 编辑员工信息
     * @param employeeEditBo
     */
    @PutMapping
    public Result editEmployee(@RequestBody @Valid EmployeeEditBo employeeEditBo,HttpServletRequest request){
        employeeService.editEmployee(employeeEditBo,request);
        return Result.success();
    }

    /**
     * 修改员工账号状态
     * @param status
     * @param id
     * @param request
     * @return
     */
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, Long id,HttpServletRequest request){
        employeeService.updateStatus(status,id,request);
        return Result.success();
    }

    /**
     * 修改密码
     * @param editPassWordBo
     * @param request
     * @return
     */
    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody @Valid EditPassWordBo editPassWordBo, HttpServletRequest request){
        employeeService.editPassword(editPassWordBo,request);
        return Result.success();
    }
}
